﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsAppweek2
{
    public partial class CustomerForm : Form
    {
        private SqlConnection con;
        SqlDataAdapter adapter;
        DataSet customerDataset;
        BindingSource bindingSource;
        public CustomerForm()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool flag = true;
            if (txtCarNo.Text == "")
            {
                errCustForm.SetError(txtCarNo, "please send a vaild car number");
                flag = false;

            }
            else
            {
                errCustForm.SetError(txtCarNo, "");
            }
            if (txtAddress.Text == "")
            {
                errCustForm.SetError(txtAddress, "please send a vaild name");
                flag = false;

            }
            else
            {
                errCustForm.SetError(txtAddress, "");
            }
            if (txtName.Text == "")
            {
                errCustForm.SetError(txtName, "please send a vaild address");
                flag = false;

            }
            else
            {
                errCustForm.SetError(txtName, "");
            }
            if (txtMake.Text == "")
            {
                errCustForm.SetError(txtMake, "please send a vaild make");
                flag = false;

            }
            else
            {
                errCustForm.SetError(txtMake, "");
            }
            if (flag = false)
                return;
            else
            {

            }
        }

        private void textBox1_LocationChanged(object sender, EventArgs e)
        {

        }

        private void CustomerForm_Load(object sender, EventArgs e)
        {



            con = new SqlConnection("Data Source=LAPTOP-ANARUPVH;Initial Catalog=CMSDatabase;Integrated Security=True;Encrypt=True;TrustServerCertificate=True");
            adapter = new SqlDataAdapter("select *from tblCustomer", con);

            customerDataset = new DataSet();

            SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(adapter);
            adapter.Fill(customerDataset,"tblCustomer");

            bindingSource = new BindingSource();
            bindingSource.DataSource = customerDataset;
            bindingSource.DataMember = "tblCustomer";

            txtCarNo.DataBindings.Add("Text", bindingSource, "CarNo");
            txtName.DataBindings.Add("Text", bindingSource, "Name");
            txtAddress.DataBindings.Add("Text", bindingSource, "Address");
            txtMake.DataBindings.Add("Text", bindingSource, "Make");

            UpdatePosition();
        }

        private void UpdatePosition()
        {
            lbPosition.Text = $"{bindingSource.Position + 1}of{bindingSource.Count}";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();

        }



        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position < bindingSource.Count - 1) {
                bindingSource.Position += 1;
            }
            UpdatePosition();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (bindingSource.Position > 0) {
                bindingSource.Position -= 1;
            }
            UpdatePosition();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DataRow newRow = customerDataset.Tables["tblCustomer"].NewRow();
            newRow["CarNo"] = "";
            newRow["name"] = "";
            newRow["address"] = "";
            newRow["make"] = "";
            customerDataset.Tables["tblCustomer"].Rows.Add(newRow);
            bindingSource.Position = bindingSource.Count - 1;
            txtCarNo.ReadOnly = false;
            txtName.ReadOnly = false;
            txtMake.ReadOnly = false;
            txtAddress.ReadOnly = false;

            UpdatePosition();


        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            try
            {
                bindingSource.EndEdit();
                adapter.Update(customerDataset, "tblCustomer");
                MessageBox.Show("success");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error");


            }
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtCarNo.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtMake.ReadOnly = false;
            txtName.ReadOnly = false;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            bindingSource.CancelEdit();

        }
    }
}
